package deibay.server.frontend;

import java.rmi.RemoteException;

import deibay.server.frontend.communication.clients.ClientsManager;
import deibay.server.frontend.communication.clients.MessageQueue;
import deibay.server.frontend.communication.mainserver.FrontendRmiServer;
import deibay.util.IClosable;

public class FrontEndCore implements IClosable{

	private ClientsManager clientsManager = null;
	private MessageQueue messageQueue = null;
	private FrontendRmiServer serverConnection = null;	
	private State state;
	
	private static FrontEndCore instance = null;
	
	public static enum State 
	{
		running,
		halt,
	};
	
	public synchronized static FrontEndCore getInstance()
	{
		if(instance == null)
		{
			instance = new FrontEndCore();
		}
		return instance;
	}
	
	private FrontEndCore()
	{
		try {
			state = FrontEndCore.State.running;
			messageQueue = new MessageQueue();		
			serverConnection = new FrontendRmiServer();
			clientsManager = new ClientsManager();			
			messageQueue.init();
			clientsManager.init();			
			serverConnection.init();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
	}	

	public ClientsManager getClientsManager() {
		return clientsManager;
	}

	public MessageQueue getMessageQueue() {
		return messageQueue;
	}

	public FrontendRmiServer getServerConnection() {
		return serverConnection;
	}
	
	public State getState()
	{
		return state;
	}
	
	public void close()
	{
		System.exit(1);
	}

	public void halt() {
		state = FrontEndCore.State.halt;
		messageQueue.close();		
		clientsManager.close();		
		serverConnection.close();		
		instance = null;
	}

	public void restart() {		
	}
}
